import time

import jinja2

from Base.BaseSetting import Best_dir, html_dir
from Base.DataCollect import DataCollect


class assertException(Exception):
    pass #Exception  底层定义了初始化方法和__str__的魔术方法，直接在实例化一个对象传入需要返回的描述对象即可
    '自定义的断言异常'
    # def __init__(self,info):
    #     pass
        # self.info = info


class locationele(Exception):
    pass
    # def __init__(self,fangfa,dingwei):
    #     self.fangfa = fangfa
    #     self.dingwei = dingwei
    # def __str__(self): # 返回一个对象的描述信息，用return返回的是一个带有描述信息的字符串
    #     return f"当前页面中不能根据{self.fangfa}找到{self.dingwei}元素"

def render_tem(result):
    env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(Best_dir),  # 模板的文件夹路径
        extensions=(),
        autoescape=True
    )
    # 通过loader传入的目录 找到目录下的  template_name
    template = env.get_template("Template.html")
    html = template.render(result=result)
    # print(html)
    with open(html_dir+ "\\Result.html", 'w', encoding="utf-8") as f:
        f.write(html)

def assemb_data(excel,filename,sheet,row ): #这里的row给的是默认值
    start_time = time.time()
    case_detail = {}
    if isinstance(row,int):   #isinstance 是判断所属类型使用的
        caseid = excel.get_caseid(row)
    elif isinstance(row,(float,str)):
        caseid = row

    caseid = int(caseid)
    case_detail['caseid'] = caseid
    case_detail['desc'] = excel.get_casedesc(caseid)
    case_detail['module'] = excel.get_casemoudle(caseid)
    DataCollect.execute_sum += 1
    try:
        excel.do_caseid(int(caseid))  # 执行do_caseid（）中的函数
        # excel.kdt.quit()  #直接退出浏览器
        case_detail['res'] = '成功'
        case_detail['res_info'] = ""
        DataCollect.success_num += 1

    except Exception as e:
        case_detail['res'] = '失败'
        case_detail['res_info'] = e
        DataCollect.fail_sum += 1
    excel.kdt.quit()  # 直接退出浏览器
    case_detail['file_path'] = f'{filename}中{sheet}'
    case_detail['run_time'] = str(round(time.time() - start_time, 1)) + "s"
    DataCollect.case_detail_list.append(case_detail)



if __name__ == '__main__':
    try:
        raise locationele('dingwei','fangfa')
    except Exception as e:
        print(e)